package testing_package;

import java.util.Deque;
import java.util.LinkedList;

public class CommonResource {
	  private Deque<String> names;  
	   
	    public CommonResource() {  
	         names = new LinkedList<String>();  
	     }  
	   
	     public synchronized void push(String name) {  
	         while (names.peek() != null) {  
	             try {  
	                 wait();  
	             } catch (InterruptedException e) {  
	             }  
	         }  
	   
	         names.push(name);  
	         System.out.println("Pushed: " + name);  
	         notifyAll();  
	     }  
	   
	     public synchronized String pop() {  
	         while (names.peek() == null) {  
	             try {  
	                 wait();  
	             } catch (InterruptedException e) {  
	             }  
	         }  
	   
	         String name = names.pop();  
	         System.out.println("Popped: " + name);  
	         notifyAll();  
	   
	         return name;  
	     }  
}
